/**
 * $Id: OrganisationTreeView.java 7 2011-03-09 07:13:54Z r.middel@gmail.com $
 *
 * Copyright (C) 2010 mrBussy.eu
 * 
 * WickedHRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * WickedHRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.mrbussy.applications.wickedhrm.gui.view;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Collection;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.IHandlerService;
import org.eclipse.ui.part.ViewPart;

import eu.mrbussy.applications.wickedhrm.data.Employee;
import eu.mrbussy.applications.wickedhrm.data.Organisation;
import eu.mrbussy.applications.wickedhrm.data.OrganizationalUnit;
import eu.mrbussy.applications.wickedhrm.data.OrganizationalUnit.OrganizationUnitType;

/**
 * @author KLC31471
 *
 */
public class OrganisationTreeView extends ViewPart {
	public static final String ID = "eu.mrbussy.applications.wickedhrm.gui.view.OrganisationTreeView";

	//private TableViewer viewer;
	private TreeViewer	treeViewer;
	/**
	 * 
	 */
	public OrganisationTreeView() {
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		Organisation KLC = new Organisation("KLM cityhopper");
		Organisation KLCBV = new Organisation("KLM cityhopper BV");
		Organisation KLCUK = new Organisation("KLM cityhopper Ltd");
		
		OrganizationalUnit depZW = KLCBV.addDepartment("ZW");
		OrganizationalUnit depZC = new OrganizationalUnit("ZC", OrganizationUnitType.FUNCTIONAL, KLCBV);
		depZW.addChild(depZC);
		Employee emp = Employee.create(31471);
		emp.setFirstname("Rudi");
		emp.setLastname("Middel");
		try {
			emp.setBirthDate("19791216");
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		depZC.addEmployee(emp);
		
		emp=Employee.create(21144);
		emp.setFirstname("Mirjam");
		emp.setLastname("Bonnema");
		depZC.addEmployee(emp);
		
		KLCBV.addDepartment("ZA");
		
		KLC.add(KLCBV);
		KLC.add(KLCUK);
		
		treeViewer = new TreeViewer(parent);
		treeViewer.setContentProvider(new TreeContentProvider());
		treeViewer.setLabelProvider(new ViewLabelProvider());
		treeViewer.setInput(new Object[] {KLC});
		treeViewer.addDoubleClickListener(new TreeViewDoubleClickListener());
		
		getSite().setSelectionProvider(treeViewer);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		treeViewer.getControl().setFocus();
	}
	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class TreeContentProvider implements ITreeContentProvider {

		@Override
		public void dispose() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public Object[] getElements(Object inputElement) {
			// Add this option to set the root object (Default not supported)
			if (inputElement instanceof Object[]) {
				return (Object[]) inputElement;
			}
	        return new Object[0];
		}

		@Override
		public Object[] getChildren(Object parentElement) {
			if (parentElement instanceof Organisation) {
				Collection<Object> tmp = new ArrayList<Object>();
				tmp.addAll(((Organisation)parentElement).getDepartments());
				tmp.addAll(((Organisation)parentElement).getChildren());
				return tmp.toArray();
			}
			if (parentElement instanceof OrganizationalUnit) {
				Collection<Object> tmp = new ArrayList<Object>();
				tmp.addAll(((OrganizationalUnit)parentElement).getEmployees().values());
				tmp.addAll(((OrganizationalUnit)parentElement).getChildren());
				return tmp.toArray();
			}
			return new Object[0];
		}

		@Override
		public Object getParent(Object element) {
			if(element instanceof OrganizationalUnit)
			{
				return ((OrganizationalUnit)element).getParents().toArray()[0];
			}
			if(element instanceof Organisation)
			{
				return ((Organisation)element).getParent();
			}
			return null;
		}

		@Override
		public boolean hasChildren(Object element) {
			if (element instanceof Organisation) {
				return ((Organisation)element).hasDepartments() || ((Organisation)element).hasChildren();
			}
			if (element instanceof OrganizationalUnit) {
				return ((OrganizationalUnit)element).hasChildren() || ((OrganizationalUnit)element).hasEmployees();
			}
			return false;
		}
		
	}

	class ViewLabelProvider extends LabelProvider implements
	ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return "test";//getText(obj);
		}
		
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		
		public Image getImage(Object obj) {
			if(obj instanceof Organisation)
				return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_DEF_VIEW);
			if(obj instanceof OrganizationalUnit)
				return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_ELEMENT);
			return null;
		}
	}
	
	class TreeViewDoubleClickListener implements IDoubleClickListener {

		@Override
		public void doubleClick(DoubleClickEvent event) {
			Object obj = ((TreeSelection)event.getSelection()).getFirstElement();
			
			// Check to see if an employee is selected
			if(obj instanceof Employee) {
				IHandlerService handlerService = (IHandlerService) getSite().getService(IHandlerService.class);
				try {
						handlerService.executeCommand("eu.mrbussy.applications.wickedhrm.core.commands.openEmployee", null);
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(
					"eu.mrbussy.applications.wickedhrm.core.commands.openEmployee");
		}
			}
		}
		
	}
}
